package com.ellen.mac.chatdemo.utils;

import android.content.Context;
import android.text.TextUtils;

import com.avos.avoscloud.AVCallback;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVUtils;
import com.avos.avoscloud.SignatureFactory;
import com.avos.avoscloud.im.v2.AVIMClient;
import com.avos.avoscloud.im.v2.AVIMException;
import com.avos.avoscloud.im.v2.AVIMMessageManager;
import com.avos.avoscloud.im.v2.AVIMTypedMessage;
import com.avos.avoscloud.im.v2.callback.AVIMClientCallback;
import com.ellen.mac.chatdemo.cache.ConversationItemCache;
import com.ellen.mac.chatdemo.cache.ProfileCache;
import com.ellen.mac.chatdemo.handler.ConversationHandler;
import com.ellen.mac.chatdemo.handler.CustomMessageHandler;
import com.ellen.mac.chatdemo.handler.ClientEventHandler;
import com.ellen.mac.chatdemo.myinterface.ChatProfileProvider;

/**
 * Created by j on 2017/12/1.
 */

public class ChatKit {
    private static ChatKit lcChatKit;
    private ChatProfileProvider profileProvider;
    private String currentUserId;
    private String currentUserName;

    private ChatKit() {
    }

    public static synchronized ChatKit getInstance() {
        if (null == lcChatKit) {
            lcChatKit = new ChatKit();
        }
        return lcChatKit;
    }

    /**
     * 初始化 LeanCloudChatKit，此函数要在 Application 的 onCreate 中调用
     *
     * @param context
     * @param appId
     * @param appKey
     */
    public void init(Context context, String appId, String appKey) {
        if (TextUtils.isEmpty(appId)) {
            throw new IllegalArgumentException("appId can not be empty!");
        }
        if (TextUtils.isEmpty(appKey)) {
            throw new IllegalArgumentException("appKey can not be empty!");
        }

        AVOSCloud.initialize(context.getApplicationContext(), appId, appKey);

        // 消息处理 handler
        AVIMMessageManager.registerMessageHandler(AVIMTypedMessage.class, new CustomMessageHandler(context));

        // 与网络相关的 handler
        AVIMClient.setClientEventHandler(ClientEventHandler.getInstance());

        // 和 Conversation 相关的事件的 handler
        AVIMMessageManager.setConversationEventHandler(ConversationHandler.getInstance(context));

        // 默认设置为离线消息仅推送数量
        AVIMClient.setOfflineMessagePush(true);
    }

    /**
     * 设置用户体系
     *
     * @param profileProvider
     */
    public void setProfileProvider(ChatProfileProvider profileProvider) {
        this.profileProvider = profileProvider;
    }

    /**
     * 获取当前的用户体系
     *
     * @return
     */
    public ChatProfileProvider getProfileProvider() {
        return profileProvider;
    }

    /**
     * 设置签名工厂
     *
     * @param signatureFactory
     */
    public void setSignatureFactory(SignatureFactory signatureFactory) {
        AVIMClient.setSignatureFactory(signatureFactory);
    }

    /**
     * 开启实时聊天
     *
     * @param userId
     * @param callback
     */
    public void open(final String userId, final String userName,final AVIMClientCallback callback) {
        open(userId, userName,null, callback);
    }

    /**
     * 开启实时聊天
     * @param userId 实时聊天的 clientId
     * @param tag 单点登录标示
     * @param callback
     */
    public void open(final String userId,final String userName, String tag, final AVIMClientCallback callback) {
        if (TextUtils.isEmpty(userId)) {
            throw new IllegalArgumentException("userId can not be empty!");
        }
        if (null == callback) {
            throw new IllegalArgumentException("callback can not be null!");
        }

        AVIMClientCallback openCallback = new AVIMClientCallback() {
            @Override
            public void done(final AVIMClient avimClient, AVIMException e) {
                if (null == e) {
                    currentUserId = userId;
                    currentUserName = userName;
                    LogUtils.v(" currentUserId "+currentUserId);
                    ProfileCache.getInstance().initDB(AVOSCloud.applicationContext, userId);
                    //todo cache
                    ConversationItemCache.getInstance().initDB(AVOSCloud.applicationContext, userId, new AVCallback() {
                        @Override
                        protected void internalDone0(Object o, AVException e) {
                            callback.internalDone(avimClient, e);
                        }
                    });
                } else {
                    callback.internalDone(avimClient, e);
                }
            }
        };

        if (AVUtils.isBlankContent(tag)) {
            AVIMClient.getInstance(userId).open(openCallback);
        } else {
            AVIMClient.getInstance(userId, tag).open(openCallback);
        }
    }

    /**
     * 关闭实时聊天
     *
     * @param callback
     */
    public void close(final AVIMClientCallback callback) {
        AVIMClient.getInstance(currentUserId).close(new AVIMClientCallback() {
            @Override
            public void done(AVIMClient avimClient, AVIMException e) {
                currentUserId = null;
                if (null != callback) {
                    callback.internalDone(avimClient, e);
                }
            }
        });
    }

    /**
     * 获取当前的实时聊天的用户
     *
     * @return
     */
    public String getCurrentUserId() {
        return currentUserId;
    }
    public String getcurrentUserName() {
        return currentUserName;
    }


    /**
     * 获取当前的 AVIMClient 实例
     *
     * @return
     */
    public AVIMClient getClient() {
        if (!TextUtils.isEmpty(currentUserId)) {
            return AVIMClient.getInstance(currentUserId);
        }
        return null;
    }
}
